# -*- coding: utf-8 -*-

#   This file is part of pynix.

# Copyright (c) 2009 - Fabiano Francesconi, Walter Da Col

# Permission is hereby granted, free of charge, to any person
# obtaining a copy of this software and associated documentation
# files (the "Software"), to deal in the Software without
# restriction, including without limitation the rights to use,
# copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the
# Software is furnished to do so, subject to the following
# conditions:

# The above copyright notice and this permission notice shall be
# included in all copies or substantial portions of the Software.

# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
# OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
# HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
# WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
# OTHER DEALINGS IN THE SOFTWARE.

import optparse
import paths
import cfg_utils

class Parser:
    def __callback_test_cfg(self, option, opt_str, value, parser):
        cfg_path = parser.values.conf_filename
        cfg_utils.test_configuration(cfg_path)

    #TODO: translate cli option
    def __add_options(self):
        ## add quiet option to suppress all output on stdout
        self.__parser.add_option('--quiet', '-q', action="store_false", dest="verbose", default=True,
            help="Suppress all output")

        ## add switch for the configuration file.
        self.__parser.add_option('--configuration-file', '-c', metavar="FILE", dest="conf_filename", type="string",
            help="use FILE instead of the default configuration file", default=paths.CONFIGURATION_PATH)

        ## add switch to select uncolored output.
        self.__parser.add_option('--no-color', '-N', action="store_false", dest="use_color", default=True,
            help="Don't output color")

        ## add switch to test configuration
        self.__parser.add_option('--test-configuration','-t', action="callback",
            callback=self.__callback_test_cfg, help='''Test the configuration. If custom configuration file is specified, -t option needs to be put after -c one;
            otherwise the specified filename won't be considered''')

    def __parse_arguments(self):
        self.__options, self.__arguments = self.__parser.parse_args()

    def __init__(self):
        self.__parser = optparse.OptionParser()
        self.__add_options()
        self.__parse_arguments()

    def is_verbose(self):
        return self.__options.verbose

    def get_cfg_path(self):
        return self.__options.conf_filename

    def is_use_color(self):
        return self.__options.use_color
